<?php
class paginator {
    const FIRSTPAGE = 1;
    const DEFAULT_PAGE_PER_LIST = 8;
    var $link = "";
    var $totalRecord;
    var $pagePerList;
    var $itemPerPage;
    var $totalPage;    
    public function  __construct($para) {
        $this->link = $para['link'];
        $this->totalRecord = $para['totalrecord'];
        $this->itemPerPage = $para['itemPerPage'];
        $this->totalPage = ceil($para['totalrecord']/$this->itemPerPage);        
        $this->pagePerList = isset($para['pagePerList'])?$para['pagePerList']: self::DEFAULT_PAGE_PER_LIST;
    }
    public function create($curPage){
       if ($this->totalPage == 1) return "";
       $first = self::FIRSTPAGE == $curPage ? "<span class='nextprev'>First</span>" : "<a href='{$this->link}/".self::FIRSTPAGE.".html' class='nextprev' title='Go to First Page'>First</a>";
       $last = $this->totalPage == $curPage ? "<span class='nextprev'>Last</span>"  : "<a href='{$this->link}/".$this->totalPage.".html' class='nextprev' title='Go to Last Page'>Last</a>";
       $result = $first;
       //---------------------------
       if ($curPage % $this->pagePerList == 0){
           $start = (floor($curPage / $this->pagePerList) -1 ) * $this->pagePerList + 1 ;
       }else{
           $start = floor($curPage / $this->pagePerList) * $this->pagePerList + 1 ;
       }
       
       if ($start == 1){
           $pre = "";
       }else{
           $pre = $start - 1;
           $pre = "<a class='nextprev' href='{$this->link}/{$pre}.html' title='Go to page {$pre}'>prev</a>";
       }

       $end = $start + $this->pagePerList - 1;
       
       if ($end >= $this->totalPage){
           $end = $this->totalPage;
           $next = "";
       }else{
            $next = $end + 1;
            $next = "<a class='nextprev' href='{$this->link}/{$next}.html' title='Go to page {$next}'>next</a>";
       }
       //--------------------------------------
       
       $result .= $pre;

       for($i = $start ; $i <= $end ; $i++){
           if ($curPage == $i){
                $result .= "<span class=\"current\">$i</span>";
           }else{
               if ($i!=1){
                    $result .= "<a href='{$this->link}/$i.html' title='Go to page $i'>$i</a>";
               }else{
                   $tmpLink = str_replace("/page", "", $this->link);
                   $result .= "<a href='{$tmpLink}.html' title='Go to page $i'>$i</a>";
               }
           }
       }

       $result .= $next;
       //---------------------------
       $result .= $last;
       return $result;
    }
}
/*
  <span class="nextprev">First</span>
  <span class="current">1</span>
  <a href="" title="Go to page 2">2</a>
  <a href="" title="Go to page 3">3</a>
  <a href="" title="Go to page 4">4</a>
  <a href="" title="Go to page 5">5</a>
  <a href="" title="Go to page 6">6</a>
  <a href="" title="Go to page 7">7</a>
  <a href="" title="Go to page 8">8</a>
  <span>…</span>
  <a href="/page230" title="Go to page 230">230</a>
  <a href="/page231" title="Go to page 231">231</a>
  <a href='/page2' class='nextprev' title='Go to Next Page'>Last</a>
 */